<template>
  <div class="wrapper">
    <swiper :options="swiperOption" v-if="showSwiper">
    <!-- v-if解决Ajax数据响应未完成时，父传子的swiperList数组为空，会导致默认显示的是最后一页的问题 -->
      <swiper-slide v-for="item of swiperList" :key="item.id">
        <img class="swiper-img" :src="item.imgUrl" />
      </swiper-slide>
      <!-- Optional controls -->
      <div class="swiper-pagination"  slot="pagination"></div>
    </swiper>
  </div>
</template>

<script>
export default {
  name: 'HomeSwiper',
  props: {
    swiperList: Array
  },
  data () {
    return {
      swiperOption: {
        pagination: '.swiper-pagination',
        // 为啥就显示轮播小点？
        loop: true,
        // 支持循环轮播
        autoplay: 3000
      }
    }
  },
  computed: {
    showSwiper () {
      return this.swiperList.length
    }
  }
}
</script>

<style lang="stylus" scoped>
  // 表示的是在.wrapee这个div下所有出现的.swiper-pagination-bullet-active，>>>表示穿透scoped限制
  // 正在显示的轮播小点设置颜色
  .wrapper >>> .swiper-pagination-bullet-active
    background: #fff
  .wrapper
    overflow: hidden
    width: 100%
    height: 0
    padding-bottom: 31.25%
    // 防止页面加载时抖动
    background: #eee
    .swiper-img
      width: 100%

</style>
